package com.chhuang.huaao.task;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.mina.core.session.IoSession;

import com.chhuang.core.holder.SpringContextHolder;
import com.chhuang.huaao.handler.ServerHandler;
import com.chhuang.huaao.po.HaMachine;
import com.chhuang.huaao.service.IHaMachineService;
import com.chhuang.system.consts.SysConstants;
import com.chhuang.utils.Assert;
import com.chhuang.utils.config.SysConfig;

/**
 * 设备离线监控任务
 * @author CHHUANG
 *
 */
public class MachineOutlineMonitoTask {
	
	private static final Log log = LogFactory.getLog(MachineOutlineMonitoTask.class);
	
	private IHaMachineService machineService = SpringContextHolder.getBean("haMachineServiceImpl");
	
	public void execute() throws Exception {
		//内存中在线设备是否已离线
		List<String> keys = new ArrayList<String>();
		for (Map.Entry<String, IoSession> entry : ServerHandler.sessionMap.entrySet()) {
			IoSession session = entry.getValue();
			if(session==null || session.isClosing() || !session.isConnected()){//客户端断开连接
				keys.add(entry.getKey());
			}
		}
		if(Assert.isValidCollection(keys)){
			for(String key : keys){
				ServerHandler.sessionMap.remove(key);
				log.debug("移除内存MAP中的会话key为："+key);
				HaMachine machine = machineService.findMachineByMachineNum(key);
				if(machine!=null){
					log.debug("移除通讯会话1 机器编号为："+machine.getMachineNum());
					machine.setMachineOnlineStatus(0);
					machine.setModifyTime(new Date());
					machineService.update(machine);
				}
			}
		}

		long now = System.currentTimeMillis();
		
		//数据库中在线设备是否已离线
		List<HaMachine> machines = machineService.findOnlineMachine();
		if(Assert.isValidCollection(machines)){
			for(HaMachine machine : machines){
				IoSession session = ServerHandler.sessionMap.get(machine.getMachineNum());
				long hearttime = machine.getHeartTime()==null ? now : machine.getHeartTime().getTime();
				hearttime = now - hearttime;
				long max = SysConfig.getLong(SysConstants.Config.MAX_HEART_TIME, 303000l);
				if(session==null || session.isClosing() || !session.isConnected() || hearttime>max){
					if(session!=null){
						session.close(true);session=null;
					}
					log.debug("移除通讯会话2 机器编号为："+machine.getMachineNum());
					machine.setMachineOnlineStatus(0);
					machine.setModifyTime(new Date());
					machineService.update(machine);
					ServerHandler.sessionMap.remove(machine.getMachineNum());
				}
			}
		}
		
		//离线时间过长的设备修改设备开关状态
		machines = machineService.findOutlineMachine();
		if(Assert.isValidCollection(machines)){
			for(HaMachine machine : machines){
				long hearttime = machine.getHeartTime()==null ? now : machine.getHeartTime().getTime();
				hearttime = now - hearttime;
				long max = SysConfig.getLong(SysConstants.Config.MAX_OUTLINE_TIME, 3600000l);
				if(hearttime > max){
					machine.setMachineOnlineStatus(null);
					machine.setMachineRunStatus(0);
					machine.setMachineRuningStatus(0);
					machine.setMachineBinStatus(0);
					machine.setMachineStoveStatus(0);
					machine.setMachineBlowerStatus(0);
					machine.setMachineWindStatus(0);
					machine.setMachinePumpStatus(0);
					machine.setMachineFireStatus(0);
					machine.setMachineWaterTemp(null);
					machine.setMachineSmokeTemp(null);
					machine.setMachineInletWaterTemp(null);
					machine.setMachineHightTempWarn(null);
					machine.setMachineLessFuelWarn(null);
					machine.setMachineLessWaterWarn(null);
					machine.setModifyTime(new Date());
					machineService.update(machine);
				}
			}
		}
	}
	
}
